﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;


namespace HTSManager
{
    [Serializable]
    class iniUtil
    {

        private string iniPath;

        public iniUtil(string path)
        {
            this.iniPath = path;  //INI 파일 위치를 생성할때 인자로 넘겨 받음
        }

        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(    // GetIniValue 를 위해
            String section,
            String key,
            String def,
            StringBuilder retVal,
            int size,
            String filePath);



        [DllImport("kernel32.dll")]
        private static extern long WritePrivateProfileString(  // SetIniValue를 위해
            String section,
            String key,
            String val,
            String filePath);

        public bool ExistINI()
        {
            return File.Exists(iniPath);
        }

        // INI 값을 읽어 온다. 
        public String GetIniValue(String Section, String Key, String DefaultValue)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, iniPath);
            string strTemp = temp.ToString();
            if (strTemp.Equals(""))
                return DefaultValue;
            return temp.ToString();
        }

        public bool GetIniValueToBoolean(String Section, String Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, iniPath);
            string strTemp = temp.ToString();
            if (strTemp.Equals("0"))
                return false;
            else if (strTemp.Equals("1") == true)
                return true;
            return false;
        }

        // INI 값을 셋팅
        public void SetIniValue(String Section, String Key, String Value)
        {
            WritePrivateProfileString(Section, Key, Value, iniPath);
        }

        public void SetIniValueToBoolean(String Section, String Key, bool Value)
        {
            string sValue;
            if (Value == true)
                sValue = "1";
            else
                sValue = "0";
            WritePrivateProfileString(Section, Key, sValue, iniPath);
        }


        public void DeleteSection(String strSection)
        {
            WritePrivateProfileString(strSection, null, null, iniPath);
        }
    }
}